package Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RadionButtonTest {
    private static void ButtonTest() {
        JFrame frame = new JFrame("单选框");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 500);
        frame.setLocation(300, 400);
        JLabel label = new JLabel("HELLO WORLD", JLabel.CENTER);
        label.setFont(new Font("宋体", Font.PLAIN, 30));
        JPanel panel = new JPanel();
        ButtonGroup buttonGroup = new ButtonGroup();
        JRadioButton radioButton1 = new JRadioButton("1");
        JRadioButton radioButton2 = new JRadioButton("2");
        panel.add(radioButton1);
        panel.add(radioButton2);
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int border = 0;
                if (radioButton1.isSelected())
                    border+=Font.BOLD;
                if (radioButton2.isSelected())
                    border+=Font.ITALIC;
                label.setFont(new Font("宋体",border,30));
            }
        };
        radioButton1.addActionListener(listener);
        radioButton2.addActionListener(listener);
        panel.add(radioButton1);
        panel.add(radioButton2);
        frame.add(panel,BorderLayout.PAGE_END);
        frame.add(label);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(RadionButtonTest::ButtonTest);
    }
}
